<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <title>路由</title>

  <script src="../js/vue.js"></script>
  <script src="../js/vue-router.js"></script>
</head>

<body>
  <div id="app">
    <router-view></router-view>
  </div>
  <script>
    Vue.component("my-header", {
      template: `
      <header>
        <h2>welcom dingding,<a href="javascript:;" @click="logout">注销</a> </h2>
        <ul>
          <li><a href="#/">首页</a></li>
          <li><router-link to="/details">详情页</router-link></li>
        </ul>
        <hr>
      </header>`,
      methods: {
        logout() {
          alert("注销成功,即将返回首页");
          this.$router.push("/");
        }
      }
    });
    var Index = {
      template: `
      <div>
        <h1 style="color:red">这是首页</h1>
        <ul>
          <li>
            <router-link to="/details/1">查看1号商品详情</router-link>
          </li>
          <li>
            <a @click="toDetails(2)">查看2号商品详情</a>
          </li>
          <li>
            <router-link to="/details/3">查看3号商品详情</router-link>
          </li>
        </ul>
      </div>
      `,
      methods: {
        toDetails(lid) {
          this.$router.push("/details/" + lid);
        }
      }
    };
    var Home = {
      template: `
        <div>
          <my-header></my-header>
          <router-view></router-view>
        </div>
      `
    }
    var Details = {
      template: `
      <div>
        <h1 style="color:orange">这是详情页</h1>
        <h2>查看{{lid}}号商品详细信息</h2>
        <h2>查看{{$route.params.lid}}号商品详细信息</h2>
      </div>
      `,
      data() {
        return {
          // lid: 0
        }
      },
      props: ["lid"], //接收参数
      created() {
        // this.lid = this.$route.params.lid;
        console.log(this.$route.params);
      }
    };
    var NotFound = {template: `<h1>404:您查找的页面不存在!</h1>`};

    var router = new VueRouter({
      routes: [{
          path: "/",
          component: Home,
          children: [{
            path: "/",
            component: Index
          }, {
            path: "/details/:lid",
            component: Details,
            props: true //传参
          }]
        },
        {
          path: "*",
          component: NotFound
        }
      ]
    });
    var vm = new Vue({
      el: "#app",
      router,
    });
  </script>
</body>

</html>